#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import pandas as pd
import talib as tl

from unit import dateneedupdate
from database import inster,last

  

def setup(df,code,type, mas=[5,10,20,30,60]):
    format = "%Y%m%d"
    path = os.path.join(
            os.path.expanduser("~"), ("stockdate/cvs/damrey/etf/setup"), code + ".csv"
        )
    needupdate = False
    startDate = ""
    lastdata = last("damrey"+type, fitter=("code='%s'" % code),orderby=["date"],asc=False)
    if lastdata == None:
        needupdate = True
    else:
        startDate = lastdata["date"]
        needupdate = dateneedupdate(startDate,format)
    if needupdate == False:
        return
    tdf = pd.DataFrame()
    tdf["code"] = df["code"]
    tdf["date"] = df["date"]
    for ma in mas:
        key = "ma"+str(ma)
        tdf[key]= tl.MA(df['close'],timeperiod=int(ma))
    tdf.to_csv(path)
    inster("damrey"+type,columns=["date","code","ma5","ma10","ma20","ma30","ma60",],df=tdf)
    print(code+"damrey signal 更新完成")